package com.jike.jkgame;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.jike.jkgame.data.Status;
import com.jike.jkgame.data.UserStatus;

public class ShortageAlertFragmentDialog extends DialogFragment implements
		OnClickListener {

	private TextView mTitleTv;
	// private TextView mValueTv;
	private TextView mTimerTv;
	private TextView mTimerTotalTv;
	private String mDialogType;
	private Status mStatus;
	private Handler mHandler = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			String name = (String) msg.obj;
			if (name != mDialogType) {
				return false;
			}
			updateTimer(msg.arg1,
					(int) (mStatus.getTotalMillisUntillFull() / 1000));

			// if(msg.what == Global.MessageType.TIME_FINISH) {
			// mValueTv.setText(mStatus.value + "/" + mStatus.maxValue);
			// }

			return false;
		}
	});

	public static ShortageAlertFragmentDialog newInstance(String type) {
		ShortageAlertFragmentDialog f = new ShortageAlertFragmentDialog();

		Bundle args = new Bundle();
		args.putString("type", type);
		f.setArguments(args);

		return f;
	}

	private void updateTimer(int remian, int totalRemain) {
		mTimerTv.setText(Utilis.fromatTime(remian));
		mTimerTotalTv.setText(Utilis.fromatTime(totalRemain));
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDialogType = getArguments().getString("type");
		mStatus = UserStatus.get().getStatusByName(mDialogType);

	}
	
	

	@Override
	public void onDestroyView() {
		mStatus.restoreTargetHandler();
		super.onDestroyView();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		getDialog().requestWindowFeature(STYLE_NO_TITLE);
		
		View v = inflater.inflate(R.layout.shortagedialog_layout, container,
				false);
		mTitleTv = (TextView) v.findViewById(R.id.tv_title);
		// mTitleTv.setText(mStatus.name);
		// mValueTv = (TextView) v.findViewById(R.id.tv_value);
		// mValueTv.setText(mStatus.value + "/" + mStatus.maxValue);
		mTimerTv = (TextView) v.findViewById(R.id.tv_timer);
		mTimerTotalTv = (TextView) v.findViewById(R.id.tv_timer_total);
		v.findViewById(R.id.btn_close).setOnClickListener(this);

		// retrieve display dimensions
		Rect displayRectangle = new Rect();
		Window window = getDialog().getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
		v.setMinimumWidth((int)(displayRectangle.width() * 0.7f));
		v.setMinimumHeight((int)(displayRectangle.height() * 0.9f));

		mStatus.pushTargetHandler(mHandler);
		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_close:
			getDialog().dismiss();
			break;
		default:
			break;
		}

	}

}
